﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using AdaptiveHelpDesk;

namespace Novice
{
    /// <summary>
    /// Summary description for authenticate
    /// </summary>
    [WebService(Namespace = "http://soaiui.nmmu.ac.za/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Login : System.Web.Services.WebService
    {
        /// <summary>
        /// 'Logs' a user into the system by comparing credentials with those stored in Database. If succesful, current user is written to the task model.
        /// </summary>
        /// <param name="Username">Username for authentication</param>
        /// <param name="Password">Password for authentication</param>
        /// <returns></returns>
        [WebMethod]
        public string login(String Username, String Password)
        {
            StoredProcedures sp = new StoredProcedures();
            ArrayList userDetails = new ArrayList();

            try
            {
                userDetails = (ArrayList)sp.getPassword(Username);

                if (userDetails.Count != 0)
                {
                    //reset working data
                    GetTaskModel gt = new GetTaskModel();
                    gt.SetCallID("");
                    gt.SetLogginDate("");
                    gt.SetLogginTime("");
                    gt.SetSelectedCustomer("");

                    string uPassword = userDetails[0].ToString();
                    string UID = userDetails[1].ToString();
                    string fName = userDetails[2].ToString();
                    string lName = userDetails[3].ToString();

                    string Stereotype = userDetails[4].ToString();

                    //write username to Task Model
                    UpdateTaskModel ut = new UpdateTaskModel();
                    ut.SetCurrentUser(UID);

                    String TodayDate = DateTime.Today.ToShortDateString().Replace('/', '-');
                    String TodayTime = DateTime.Now.ToLongTimeString();

                    gt.SetLogginDate(TodayDate);
                    gt.SetLogginTime(TodayTime);

                    return "true";
                }
                else
                {
                    return "false";
                }
            }
            catch (Exception ex)
            {
                return "Unknown error occured: " + ex.Message;
            }
        }

        /// <summary>
        /// Check Task Model if user is logged in. If yes, return the username, else return string 'false'
        /// </summary>
        /// <param name="Username"></param>
        /// <returns>'true' if logged in 'false' if not </returns>
        [WebMethod]
        public string loggedIn()
        {
            GetTaskModel gt = new GetTaskModel();
            String username = gt.CheckCurrentUser();

            if (username.Equals(""))
                return "false";
            else
                return username;
        }

        /// <summary>
        /// Gets the Stereotype (Novice / Expert) of the current user)
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string getStereoptype()
        {
            StoredProcedures sp = new StoredProcedures();
            ArrayList userDetails = new ArrayList();
            string username = loggedIn();

            userDetails = (ArrayList)sp.getPassword(username);

            if (userDetails.Count != 0)
            {
                string uPassword = userDetails[0].ToString();
                string UID = userDetails[1].ToString();
                string fName = userDetails[2].ToString();
                string lName = userDetails[3].ToString();

                string Stereotype = userDetails[4].ToString();

                return Stereotype;
            }
            else
            {
                return "User not found";
            }
        }
    }
}
